// page p0000 controller
var p0000Controller = (function($) {

	var key = 'p0000';
	var selector = '#' + key;
	var page = 'pages/' + key + '.html';

	var isPendingToShowPassword = false;

	// assume item like { key: xxx, userName: xxx, password: xxx, remark: xxx }
	var drawItem = function (item) {
		if (typeof item === 'undefined' || !item)
			return '';

		return '' +
			'<div class="p0000Item" tagKey="' + item.key + '" ' +
				'onclick="p0000Controller.itemClick(this)">' +
				'<div class="p0000Perface p0000DeleteButton" onclick="p0000Controller.btnDeleteClick(\'' + item.key + '\')"></div>' +
				'<div class="p0000Perface p0000ModifyButton" onclick="p0000Controller.showDetail(\'' + item.key + '\', true)"></div>' +
				'<div class="p0000Perface p0000KeyDiv">' + item.key + '</div>' +
				'<div class="p0000Backface">' +
					'<div class="p0000UserNameDiv">' + maskPassword(item.userName) + '</div>' +
					'<div class="p0000PasswordDiv">' + maskPassword(item.password) + '</div>' +
					'<div class="clear"></div>' +
				'</div>' +
				'<div class="p0000Perface p0000RemarkDiv">' + item.remark + '</div>' +
				'<div class="clear"></div>' +
			'</div>';
	};

	var maskPassword = function (pwd) {
		if (!pwd)
			return pwd;

		if (typeof pwd != 'string')
			return pwd;

		var b = true, c = 0;
		var arr = [];
		for (var i=0; i<pwd.length; i++) {
			arr.push(b ? pwd[i] : '<span class="p0000Mask">*</span>');

			c = c+1;
			if (c < 3)
				continue;

			c = 0;
			b = !b;
		}
		return arr.join('');
	};

	var showPassword = function (element) {
		$(element).find('.p0000Perface').hide();
		$(element).find('.p0000Backface').show();
	};

	var hidePassword = function (element) {
		$(element).find('.p0000Perface').show();
		$(element).find('.p0000Backface').hide();
	};

	var showDetail = function (key, modify) {
		event.stopPropagation();
		p0002Controller.showDialog(function (cancelled, data) {
			if (cancelled)
				return;

			PlatformSupport.writeItem(data.key, data, function (success, message) {
				if (!success) {
					alert('写入失败: ' + message);
					return;
				}

				fetch();
			});
		}, { key: key, editable: !key || modify, create: !key });
	};

	var deleteItem = function (itemKey) {
		PlatformSupport.deleteItem(itemKey, function () {
			fetch();
		});
	};

	var draw = function (items) {
		var html = [];
		for (var i=0; i<items.length; i++) {
			var item = items[i];
			html.push(drawItem(item));
		}

		if (html.length == 0)
			$('#p0000Hint').show();
		else
			$('#p0000Hint').hide();

		html.push('<div class="clear"></div>');
		$('#p0000Container').html(html.join('\r\n'));
	};

	var fetch = function () {
		PlatformSupport.listItems(function (items) {
			draw(items);
		});
	};

	var show = function () {
		$.esPage.moveTo(selector);
		app.afterLoad(selector);
		fetch();
	};

	var obj = {

		init: function (callback) {
			app.loadPage(page, function () {
				$(key).esPage();
				if (callback)
					callback();
			});
		},

		refresh: fetch,
		show: show,
		showDetail: showDetail,

		itemClick: function (element) {
			event.stopPropagation();
			showPassword(element);
			setTimeout(function () {
				hidePassword(element);
			}, 1000);
		},

		btnAddClick: function () {
			event.stopPropagation();
			showDetail(null);
		},

		btnQuitClick: function () {
			event.stopPropagation();
			PlatformSupport.exit();
		},

		btnDeleteClick: function (itemKey) {
			event.stopPropagation();
			if (!confirm('确认删除项目"' + itemKey + '"的密码吗?'))
				return;

			deleteItem(itemKey);
		}

	};

	app.registerPageController(key, obj);

	return obj;

})(jQuery);